package com.example.zhangch.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobQuery.CachePolicy;
import cn.bmob.v3.datatype.BmobDate;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.CountListener;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import android.util.Log;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import  com.panxw.android.imageindicator.ImageIndicatorView;
import android.support.v7.app.AppCompatActivity;
import com.panxw.android.imageindicator.AutoPlayManager;
import com.panxw.android.imageindicator.ImageIndicatorView;
import java.util.ArrayList;
import java.util.List;

import com.example.zhangch.myapplication.GalleryAdapter.OnItemClickListener;
public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private GalleryAdapter mAdapter;
    private List<String> mDatas;
    ImageIndicatorView imageIndicatorView;
    final List<String> urlList= new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //第一：默认初始化
        Bmob.initialize(this, "39dfc34280e39b5411c3e450466da0f2");

        mDatas= new ArrayList<String>();
        Querycategory();
        //得到控件


    }

    private  void Querycategory()
    {
        BmobQuery<dongimgcategory> query = new BmobQuery<dongimgcategory>();
        //查询playerName叫“比目”的数据
        query.addWhereEqualTo("isinuse", "true");
     //返回50条数据，如果不加上这条语句，默认返回10条数据
       // query.setLimit(50);
     //执行查询方法
        query.findObjects(new FindListener<dongimgcategory>() {
            @Override
            public void done(List<dongimgcategory> object, BmobException e) {
                if(e==null){
                    //toast("查询成功：共"+object.size()+"条数据。");
                    for (dongimgcategory categoru : object) {
                        //获得playerName的信息
                        mDatas.add(categoru.getcategoryname());
                        Log.i("bmob","chenggong："+categoru.getcategoryname()
                        );
                    }
                    mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview_horizontal);
                    //设置布局管理器
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager( getBaseContext());
                    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    mRecyclerView.setLayoutManager(linearLayoutManager);
                    //设置适配器
                    mAdapter = new GalleryAdapter( getBaseContext(), mDatas);
                    mRecyclerView.setAdapter(mAdapter);
                    mAdapter.setOnItemClickListener(new OnItemClickListener(){
                        @Override
                        public void onItemClick(View view , int position){
                            Toast.makeText(MainActivity.this, mDatas.get(position),5000).show();
                            Queryimagedetail(mDatas.get(position));

                        }
                    });
                }else{
                    Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }
     private  void Queryimagedetail(String categoryname)
     {
         BmobQuery<imageinfo> imageinfoquery = new BmobQuery<imageinfo>();
         //查询playerName叫“比目”的数据
         imageinfoquery.addWhereEqualTo("imagecategory",categoryname);
         imageinfoquery.findObjects(new FindListener<imageinfo>() {
             @Override
             public void done(List<imageinfo> object, BmobException e) {
                 if (e == null) {
                     //toast("查询成功：共"+object.size()+"条数据。");
                     for (imageinfo categoru : object) {
                         //获得playerName的信息
                         urlList.add(categoru.getimageurl());
                         Log.i("category", "category：" + categoru.getimageurl()
                         );
                     }
                     imageIndicatorView = (ImageIndicatorView) findViewById(R.id.network_indicate_view);
                     imageIndicatorView.setupLayoutByImageUrl(urlList);
                     imageIndicatorView.show();

                     //设置自动播放
                     AutoPlayManager autoBrocastManager =  new AutoPlayManager(imageIndicatorView);
                     autoBrocastManager.setBroadcastEnable(true);
                     autoBrocastManager.setBroadCastTimes(5);//循环次数设置
                     autoBrocastManager.setBroadcastTimeIntevel(500, 500);
                     autoBrocastManager.loop();

                 } else {
                     Log.i("category", "失败：" + e.getMessage() + "," + e.getErrorCode());
                 }
             }
         });
     }
}
